#!/bin/bash


export PATH=/usr/bin:/bin

MASTERS="phxitdns01.vip.its.ebay.com phxitdns02.vip.its.ebay.com denitdns01.vip.its.ebay.com denitdns02.vip.its.ebay.com"
LOG="/ebay/bind-logs/bind-misc.log /ebay/bind-logs/bind-misc.log.0"
DIG=/opt/bind/bin/dig

DATE=$(date "+%d-%b-%Y")
#DATE="11-Apr-2013"

. /etc/named/IT_DNS_SERVERS


zones=$(
for master in $MASTERS; do
#	echo -e "grep \"^$DATE.*expired$\" $LOG" | ssh $master bash | sed -e "s/.* general: zone //" -e "s/\/IN: expired//"
	echo -e "grep \": expired$\" $LOG" | ssh $master bash | sed -e "s/.* general: zone //" -e "s/\/IN: expired//"
done | sort | uniq
)

#echo $zones; exit 


if [ "X$zones" == "X" ]; then exit 0; fi

typeset -i no=$(echo $zones | sed -e "s/[A-z0-9\.\_]//g" -e 's/\-//g' | wc -c)

displayhead="Y"
expired=""
for zone in $(echo $zones); do
	/usr/local/sbin/zonecat | /usr/xpg4/bin/grep -q  "^${zone}[[:space:]]\{1,\}"
	if [ $? -ne 0 ]; then continue; fi
	catagory=$(/usr/local/sbin/zonecat | /usr/xpg4/bin/grep "^${zone}[[:space:]]\{1,\}" | awk '{print $2}')
	if [ $catagory == "stub" ]; then continue; fi
	errors=""
	for server in $(echo $DNS_PRIMARY_MASTER $DNS_PSEUDO_MASTERS $DNS_SLAVES); do
		### if [ $server == "phxitdns03.phx.its.ebay.com" -o $server == "phxitdns04.phx.its.ebay.com" ]; then 
		### 	if [ "X${catagory}" == "Xnodb" ]; then
		### 		result="good"
		### 	else
		### 		result=$($DIG @${server} -t soa $zone +short)
		### 	fi
		### else
			result=$($DIG @${server} -t soa $zone +short)
		### fi
		if [ "X${result}" == "X" ]; then errors="$errors\n\texpired on $server"; fi

	done
	if [ "X${errors}" != "X" ]; then
		if [ $displayhead == "Y" ]; then 
			echo -e "\nVerfiying $no zones expired logged on one or more masters - $MASTERS\n"
			displayhead="N"
		fi
		echo -e "\nVerifying zone \"$zone\"... "
		echo -e "zone slaved from: \c"
		/usr/local/sbin/zonecat | /usr/xpg4/bin/grep "^${zone}[[:space:]]\{1,\}" | awk '{print $3}'
		echo -e "$errors"
		if [ "X$expired" == "X" ]; then
			expired="^$zone[[:space:]]"
		else
			expired="${expired}|^${zone}[[:space:]]"
		fi
		#echo $expired
	fi
done > /tmp/$$.zones.expired

		
if [ $displayhead == "N" ]; then
	echo -e "\n\n=======================================================================================================================\n\n\n\t\tProperties of Expired Zones\n\nZone\t\t\tMasters\t\t\tCatagory\n----------------------------------------------------------------------------------" >> /tmp/$$.zones.expired
	/usr/local/sbin/zonecat | /usr/xpg4/bin/egrep $expired | sed -e 's/\/[a-z].*//' | awk '{print $1 "\t" $3 "\t" $4}' |  sort -k2 >> /tmp/$$.zones.expired


cat >>   /tmp/$$.zones.expired << \END_of_NOTES


=======================================================================================================================


				NOTES

0. take this alert as high priorty if a major zone is expired, a major zone could be one of the following:
. ebay.com paypal.com
. [vip|sjc|slc|phx|den|smf].ebay.com
. [vip|phx|slc|den].paypal.com
. corp.ebay.com tpa.ebay.com
. stubcorp.com stubprod.com billmelater.com

1. if a bunch of "mp" or "qa" zones are expired, run "named_process_zones -mp" or "named_process_zones -qa" and check in their new zone configuration, and then rerun this tool.

2. find out the cause of zone expiration on one or more masters: 
# grep "Zone/IN" /ebay/bind-logs/bind-misc.log.0 /ebay/bind-logs/bind-misc.log | less

3. verify the zone by querying the zone on their masters and doing a trial zone transfer:
# zonecat  | grep "Masters"
# dig @master -t soa Zone +short
# dig @master -t axfr Zone +short

verify accessibility of masters of a slave zone further on the primary master: 
# verify-a-3pp-zone Zone "master1 master2"

4. follow up with NetSec on F/W rules(routes) if the remote master is still alive but not reachable.

5. ignore one zone expired due to the zone missing in their masters, or, zone transfer refused by their masters. 

END_of_NOTES

	# cat /tmp/$$.zones.expired | mailx -s "Zone Expiration Check" yajin@ebay.com
	cat /tmp/$$.zones.expired | mailx -s "[unixmon]: Zone Expiration Check" itunix@ebay.com
fi

rm -f /tmp/$$.zones.expired
